package com.jyg.common.exception;


import com.jyg.common.result.Result;
import com.jyg.common.result.ResultEnum;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Slf4j
public class CustomException extends RuntimeException {
    private Result<?> result;

    public CustomException(String msg, Throwable e) {
        super(msg, e);
        Result<Object> result = new Result<>(ResultEnum.FAILED);
        result.setMsg(msg);
        result.setData(e);
        this.result = result;
    }

    public CustomException(String msg) {
        super(msg);
        Result<Object> result = new Result<>(ResultEnum.FAILED);
        result.setMsg(msg);
        this.result = result;
    }


    public CustomException(ResultEnum resultEnum) {
        super(resultEnum.getMsg());
        this.result = new Result<>(resultEnum);
    }

    /**
     * 无参异常
     *
     * @return
     */
    public static CustomException err() {
        return new CustomException(new Result<>(ResultEnum.FAILED));
    }

    /**
     * 提示类型的异常
     *
     * @param msg
     * @return
     */
    public static CustomException hint(String msg) {
        Result<?> result = new Result<>(ResultEnum.HINT);
        result.setMsg(msg);
        return new CustomException(result);
    }

    /**
     * 打印debug日志的异常
     *
     * @param msg
     * @return
     */
    public static CustomException debug(String msg) {
        Result<?> result = new Result<>(ResultEnum.DEBUG);
        result.setMsg(msg);
        return new CustomException(result);
    }
}
